package cn.edu.nju.askinginhand.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import cn.edu.nju.askinginhand.util.JSONParser;
/**
 * 
 * @author ZhangZu
 *
 */
public class QuesHistoryActivity extends Activity {
	TextView quesHistoryText;
	ListView quesHistoryList;
	List<Map<String, Object>> list;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ques_history);
		quesHistoryText = (TextView) findViewById(R.id.quesHistoryText);
		quesHistoryList = (ListView) findViewById(R.id.quesHistoryList);
		
		initHistoryList();
	}
	
	public void initHistoryList() {
		SimpleAdapter expAdapter = new SimpleAdapter(QuesHistoryActivity.this, getData(),
				R.layout.ques_history_item, new String[] { "quesImage", "quesTitle","quesTime" },
				new int[] { R.id.quesImage,R.id.quesTitleText,R.id.quesTimeText});
		quesHistoryList.setAdapter(expAdapter);		
		
		quesHistoryList.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View v, int pos,
					long id) {
			/*	String strId=(Integer)list.get(pos).get("quesId")+"";
				Toast.makeText(QuesHistoryActivity.this, strId, Toast.LENGTH_SHORT).show();
				Bundle bundle = new Bundle();
				bundle.putString("id", strId);*/
				/**
				 * TODO: check the question
				 */
/*				Intent intent = new Intent(QuesHistoryActivity.this,
						CheckQues.class);
				intent.putExtras(bundle);
				startActivity(intent);*/
			}
		});
	}
	
	private List<Map<String, Object>> getData() {
		// Test Data
/*		for(int i = 0 ; i <2;i++) {
			Map<String, Object> ques = new HashMap<String, Object>();
			ques.put("quesId",i);//replace i by Real Id here
			ques.put("quesTitle","textTitle");
			ques.put("quesTime","2011/5/10");
			list.add(ques);
		}*/
		
		// server data
		List <NameValuePair> params = new ArrayList <NameValuePair>();
		params.add(new BasicNameValuePair("uid", LoginActivity.uid));
		System.out.println(LoginActivity.uid);
		String raw = JSONParser.getOut("QuesReturnAction"+LoginActivity.filter,params);
		System.out.println(raw);
		String quesListStr = JSONParser.removeRoot(raw, "result");
		List<Map<String, String>> serverList = JSONParser.getList(quesListStr);
		list = new ArrayList<Map<String, Object>>();
		System.out.println(serverList.size());
		for(int i = 0 ; i < serverList.size();i++) {
			Map<String, String> serverQues = serverList.get(i);
		    Map<String, Object> ques = new HashMap<String, Object>();
			ques.put("quesImage", R.drawable.q);
			ques.put("quesTitle",serverQues.get("title"));
			ques.put("quesTime",serverQues.get("time"));
			ques.put("quesId",serverQues.get("id"));//replace i by Real Id here
			list.add(ques);
		}
		return list;
	}
}
